﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.BOMguans;
using System.Data;

namespace MyMES.Writer.API.Application.CommendHandlers.BOMguans
{
    public class AddBOMguanHandler : IRequestHandler<AddBOMguancommand, APIResult<BOMguan>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BOMguan> baseRepository;
        private readonly DatabaseContext db;

        public AddBOMguanHandler(IMapper mapper,IBaseRepository<BOMguan> baseRepository,DatabaseContext db)
        {
            this.mapper = mapper;
            this.baseRepository = baseRepository;
            this.db = db;
        }
        public async Task<APIResult<BOMguan>> Handle(AddBOMguancommand request, CancellationToken cancellationToken)
        {
        
                    try
                    {
                        APIResult<BOMguan> result = new APIResult<BOMguan>();
                        result.Code = APIEnum.添加成功;
                        result.Msg = "添加成功";
                        var aa = mapper.Map<BOMguan>(request);
                        var sql = "insert into  BOMguan (BomNo,BomTheme,BomDefault,BomVersion,BomType,BomName,DayNum,BomNote,WIds,GIds,Attachment,IsDeleted) values (@BomNo,@BomTheme,@BomDefault,@BomVersion,@BomType,@BomName,@DayNum,@BomNote,@WIds,@GIds,@Attachment,@IsDeleted)";
                        await baseRepository.Insert(aa, sql);
                        return result;
                    }
                    catch (Exception)
                    {

                        throw;
                    }
              
        }
    }
}
